package jp.go.aist.six.test.util.castor.persist;

import jp.go.aist.six.util.castor.CastorPersistable;
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.Persistent;
import org.exolab.castor.mapping.AccessMode;
import java.util.Vector;

public class Product
extends CastorPersistable<Integer>
implements Persistent {
    private int          _id;

    private String       _name;

    private float        _price;

    private ProductGroup _group;

    private Vector<ProductDetail>       _details = new Vector<ProductDetail>();

    private Vector<Category>       _categories = new Vector<Category>();


    public Product()
    {

    }


    public Product(
                    final int id,
                    final String name,
                    final float price,
                    final ProductDetail[] details,
                    final ProductGroup group,
                    final Category[] categories
                    )
    {
        setId( id );
        setName( name );
        setPrice( price );
        setGroup( group );

        if (details != null) {
            for (ProductDetail  d : details) {
                addDetail( d );
            }
        }

        if (categories != null) {
            for (Category  c : categories) {
                addCategories( c );
            }
        }
    }


    public int getId() {
        return _id;
    }

    public void setId(final int id) {
        _id = id;
    }

    public String getName() {
        return _name;
    }

    public void setName(final String name) {
        _name = name;
    }

    public float getPrice() {
        return _price;
    }

    public void setPrice(final float price) {
        _price = price;
    }

    public ProductGroup getGroup() {
        return _group;
    }

    public void setGroup(final ProductGroup group) {
        _group = group;
    }

    public ProductDetail createDetail() {
        return new ProductDetail();
    }

    public Vector<ProductDetail> getDetails() {
        return _details;
    }


    public void addDetail(final ProductDetail detail) {
        _details.add( detail );
        detail.setProduct( this );
    }

    public Vector<Category> getCategories() {
        return _categories;
    }

    public void addCategories(final Category category) {
        if ( ! _categories.contains( category ) ) {
            _categories.addElement( category );
            category.addProduct( this );
        }
    }



    // AbstractPersistable //

    @Override
    public void setPersistentID(
                    final Integer id
                    )
    {
        if (id != null) {
            setId( id.intValue() );
        }
    }


    @Override
    public Integer getPersistentID()
    {
        return (new Integer( getId() ));
    }


    // Castor Persistent //

    public void jdoPersistent(final Database db) { }

    public void jdoTransient() { }

    public Class<? extends Product> jdoLoad(final AccessMode accessMode) {
        if ( _name.indexOf("PC") >= 0 ) {
            return Computer.class;
        }
        return null;
    }

    public void jdoModify() { }

    public void jdoBeforeCreate(final Database db) { }

    public void jdoAfterCreate() { }

    public void jdoStore(final boolean modified) { }

    public void jdoBeforeRemove() { }

    public void jdoAfterRemove() { }

    public void jdoUpdate() { }

    public String toString() {
        return "Product-" + System.identityHashCode( this )
        + "<id: " + _id + ", name: " + _name + ", price: " + _price
        + ", details: " + getDetails()
        + ", group: " + getGroup()
        + ", categories: " + getCategories()
        + ">";
    }

}
